package com.jblv.frame.user.service.bll.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.jblv.frame.common.enums.ExceptionEnum;
import com.jblv.frame.common.exception.FrameException;
import com.jblv.frame.common.type.BooleanType;
import com.jblv.frame.common.utils.UUIDGenerator;
import com.jblv.frame.user.api.bll.ISysRoleBLL;
import com.jblv.frame.user.api.entity.SysRole;
import com.jblv.frame.user.api.service.ISysRoleService;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

/**
 * @Description 管理员角色业务逻辑类
 * @Author  yangtao
 * @Create  2020-04-21
 **/
@Component
@Service
public class SysRoleBLLImpl implements ISysRoleBLL {

    @Autowired
    private ISysRoleService sysRoleService;

    private static Logger logger = LoggerFactory.getLogger(SysRoleBLLImpl.class);


    @Override
    public SysRole get(String id) {

//        String key = BaseCacheUtils.getBeanCacheKeyById(id);
        SysRole bean = null;//BaseCacheUtils.get(key);
        if (bean != null) {
            return bean;
        }
        EntityWrapper<SysRole> wrapper = new EntityWrapper<>();
        wrapper.eq("DELETE_FLAG", BooleanType.FALSE.getCode());
        wrapper.eq("ROLE_ID",id);
        bean = sysRoleService.selectOne(wrapper);
        if (bean != null) {
            logger.info("add cache");
            //addCache(bean);
        }

        return bean;
    }


    @Override
    public String save(String accountId, SysRole sysRole) {
        if (sysRole == null) {
            return null;
        }
        if (StringUtils.isEmpty(sysRole.getRoleId())) {
            sysRole.setRoleId(UUIDGenerator.getUUID());
        }

        sysRole.setCreator(accountId);
        sysRole.setCreateTime(System.currentTimeMillis());
        sysRole.setDeleteFlag(BooleanType.FALSE.getCode());

        boolean save = false;
        try {
            save = sysRoleService.insert(sysRole);
        }catch (Exception e){
            logger.error("插入错误:{}",e.getMessage());
        }
        if (save) {
            //addCache(sysRole);
            return sysRole.getRoleId();
        }

        return null;
    }

    @Override
    public boolean update(String operatorId, SysRole sysRole) {

        SysRole sysRoleCheck = get(sysRole.getRoleId());
        if (sysRoleCheck != null) {
            throw new FrameException(ExceptionEnum.PARAM_ERROR);
        }
        if (sysRole != null) {

            sysRole.setUpdater(operatorId);

            boolean flag = sysRoleService.updateById(sysRole);
            if (flag) {
                //SysRole newBean = sysRoleService.selectById(sysRole.getId());
                //updateCache(oldUserInfo, newBean);
                return true;
            }
        }
        return false;
    }

    @Override
    public boolean remove(String operatorId, String id) {

        if (StringUtils.isEmpty(id)) {
            return false;
        }
        SysRole sysRole = get(id);
        if (sysRole != null) {
            SysRole sysRoleUpdate = new SysRole();
            sysRoleUpdate.setDeleteFlag(BooleanType.TRUE.getCode());
            sysRoleUpdate.setUpdater(operatorId);
            sysRoleUpdate.setRoleId(id);
            Boolean flag = sysRoleService.updateById(sysRoleUpdate);
            if (flag) {
                //removeCache(bayonetInfo);
                return true;
            }
        }
        return false;
    }



}
